
<template>
  <div class="execution">
    <basic-container>
      <avue-crud
        ref="crud"
        :page="page"
        :data="tableData"
        :permission="permissionList"
        :table-loading="tableLoading"
        :option="tableOption"
        @on-load="getList"
        @search-change="searchChange"
        @search-reset="searchReset"
        @refresh-change="refreshChange"
        @row-update="handleUpdate"
        @row-save="handleSave"
        @row-del="rowDel"
      >
        <!-- <template
          slot="searchMenu"
          slot-scope="{row,size}"
        >
          <el-button
            v-if="permissionList.export"
            @click="exportClick(row)"
          >导出</el-button>
        </template> -->
        <template slot="menuRight">
          <el-button
            v-if="permissionList.export"
            @click="exportClick"
          >导出</el-button>
        </template>
        <template slot="differenceAmountSearch">
          <div>
            <el-input-number
              placeholder="最小值"
              style="width: 45%"
              :controls="false"
              v-model="extraParam.startMoney"
            ></el-input-number>
            -
            <el-input-number
              placeholder="最大值"
              style="width: 45%"
              :controls="false"
              v-model="extraParam.endMoney"
            ></el-input-number>
          </div>
        </template>
      </avue-crud>
    </basic-container>
  </div>
</template>

<script>
import { fetchList, getObj, addObj, putObj, delObj, dataExport } from '@/api/rca/swckreportreview'
import { tableOption } from '@/const/crud/rca/swckreportreview'
import { mapGetters } from 'vuex'
import { handleExcelResponse, getQueryString } from "@/util/util";

export default {
  name: 'swckreportreview',
  data () {
    return {
      searchForm: {
      },
      extraParam: {
        startMoney: undefined,
        endMoney: undefined,
        mainReportEndDate: undefined,
        mainReportStartDate: undefined,
        recordDiffEndDate: undefined,
        recordDiffStartDate: undefined,
      },
      tableData: [],
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 20 // 每页显示多少条
      },
      tableLoading: false,
      tableOption: tableOption
    }
  },
  computed: {
    ...mapGetters(['permissions']),
    permissionList () {
      return {
        export: this.vaildData(this.permissions.rca_swckreportreview_export_excel, false),
        addBtn: this.vaildData(this.permissions.rca_swckreportreview_add, false),
        editBtn: this.vaildData(this.permissions.rca_swckreportreview_edit, false)
      };
    }
  },
  methods: {
    async getList (page, params) {
      this.tableLoading = true;
      const [mainReportStartDate, mainReportEndDate] = this.searchForm.mainReportCreateTime || []
      const [recordDiffStartDate, recordDiffEndDate] = this.searchForm.recordDifferenceTime || []

      try {
        const response = await fetchList(
          Object.assign(
            {
              current: page.currentPage,
              size: page.pageSize,
            },
            params,
            this.searchForm,
            {
              mainReportStartDate,
              mainReportEndDate,
              recordDiffStartDate,
              recordDiffEndDate,
            }
          )
        );
        this.tableData = response.data.data.records;
        this.page.total = response.data.data.total;
        this.tableLoading = false;
      } catch (e) {
        this.tableLoading = false;
      }
    },
    rowDel: function (row, index) {
      var _this = this
      this.$confirm('是否确认删除ID为' + row.id, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function () {
        return delObj(row.id)
      }).then(data => {
        _this.$message.success('删除成功')
        this.refreshChange()
      })
    },
    handleUpdate: function (row, index, done, loading) {
      putObj(row).then(data => {
        this.$message.success('修改成功')
        done()
        this.refreshChange()
      }).catch(() => {
        loading();
      });
    },
    handleSave: function (row, done, loading) {
      addObj(row).then(data => {
        this.$message.success('添加成功')
        done()
        this.refreshChange()
      }).catch(() => {
        loading();
      });
    },
    async searchChange (form, done) {
      this.searchForm = { ...form, ...this.extraParam };
      try {
        await this.getList(this.page, this.searchForm);
      } finally {
        done();
      }
    },
    searchReset () {
      Object.assign(this.extraParam, {
        startMoney: undefined,
        endMoney: undefined,
        mainReportEndDate: undefined,
        mainReportStartDate: undefined,
        recordDiffEndDate: undefined,
        recordDiffStartDate: undefined,
      });

      this.page.currentPage = 1
      this.getList(this.page);
    },
    refreshChange () {
      this.getList(this.page)
    },
    async exportClick (row) {
      // await this.searchChange(row, () => { });
      this.dataExport();
    },
    async dataExport () {
      let loading = this.$loading({
        lock: true,
        text: "Loading",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)"
      })
      try {
        const res = await dataExport({...this.extraParam, ...this.searchForm});
        // handleExcelResponse(res, `报表间数据关联审核导出.xlsx`);
        handleExcelResponse(res);
      } finally {
        loading.close();
      }
    },
  }
}
</script>
